import axios from 'axios'
import { Toast } from "vant";
import { UserModule } from "@/store/modules/user";
import { Get_App_Token_Key } from "@/utils/auth";


// const { message } = getCurrentInstance().appContext.config.globalProperties
// create an axios instance
const service = axios.create({
  headers: { 'Content-Type': 'application/json;charset=UTF-8' },
  // baseURL: `${url}:7000/`
  baseURL: `api/`
  // timeout: 30000
})
// request interceptor
service.interceptors.request.use(
  (config: any) => {
    const token = Get_App_Token_Key()[UserModule.userAddr]
    if (token) {
      const token_type = "Bearer"
      config.headers.Authorization = token_type + ' ' + token
    }
    return config
  },
  (error: any) => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  (response: any) => {
    // if ([1001, 1002, 1003, 1004, 1005].indexOf(response.data.code) != -1) {
    //   getSign()
    // }
    // if (needs.indexOf(response.config.url) != -1 && response.data.code !== 20) {
    //   getSign()
    // }
    if (response.status !== 200) {
      Toast.clear()
      // Dialog.alert({
      //   title: '网络错误'
      // }).then(() => {
      // })
    } else {
      return response
    }
  }, (error: any) => {
    Toast.clear()
    // Dialog.alert({
    //   title: '网络错误'
    // }).then(() => { 
    // })
    return Promise.reject(error)
  }
)
export default service